import express, { Router } from 'express';
import serverless from 'serverless-http';

import mockList from '../../mock/index';

const api = express();

const router = Router();
mockList.forEach((config) => {
  const method = (config.method || 'get') as 'get' | 'post' | 'put' | 'delete';
  router[method](config.url, (req, res) => {
    if (typeof config.response === 'function') {
      res.json(config.response(req, res));
    } else {
      res.json(config.response);
    }
  });
});
api.use('/', router);

export const handler = serverless(api);
